package cn.com.lsy.product.lepao.controller;

import cn.com.lsy.product.lepao.common.Constant;
import cn.com.lsy.product.lepao.controller.base.BaseController;
import cn.com.lsy.product.lepao.model.entities.LpDataTotalTbl;
import cn.com.lsy.product.lepao.model.entities.LpStoreDataTotalTbl;
import cn.com.lsy.product.lepao.request.ActionLibraryInfoParam;
import cn.com.lsy.product.lepao.request.IndexTotalParam;
import cn.com.lsy.product.lepao.response.IncomeTotalRet;
import cn.com.lsy.product.lepao.response.IndexStoreTotalRet;
import cn.com.lsy.product.lepao.response.IndexTotalRet;
import cn.com.lsy.product.lepao.response.IndexUserTotalRet;
import cn.com.lsy.product.lepao.response.base.ApiResponseEnum;
import cn.com.lsy.product.lepao.response.base.BaseRet;
import cn.com.lsy.product.lepao.service.IDataTotalService;
import cn.com.lsy.product.lepao.util.CommonFunctionUtil;
import cn.com.lsy.product.lepao.util.DozerMapperUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.log4j.Logger;
import org.dozer.DozerBeanMapper;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Api(value = "统计接口", tags = "统计接口")
@RestController
@RequestMapping(value = "/lepao/console/doAuth")
public class TotalController extends BaseController {

    private Logger log = Logger.getLogger(TotalController.class);
    Mapper mapper = new DozerBeanMapper();

    @Autowired
    IDataTotalService iDataTotalService;

    @RequestMapping(value = "/indexTotal", method = RequestMethod.POST)
    @ApiOperation(notes = "主页平台数据统计", httpMethod = "POST", value = "主页平台数据统计")
    public BaseRet<IndexTotalRet> indexTotal(@ApiParam(value = "请求参数", required = true) @Valid @RequestBody IndexTotalParam param,
                                             BindingResult result) {

        BaseRet<IndexTotalRet> ret = new BaseRet<IndexTotalRet>();

        IndexTotalRet indexTotalRet = new IndexTotalRet();

        try {

            //当天时间
            String currentDate = CommonFunctionUtil.getStringTime(Constant.formatType.date);
            String startTime = currentDate + " 00:00:00";
            String endTime = currentDate + " 23:59:59";

            if (param.getStoreId().size() <= 0) {
                param.setStoreId(null);
            }


            //今日新增用户
            BigDecimal currentNewUser = iDataTotalService.selectNewRegisterTotal(startTime, endTime);
            //今日活跃用户
            int currentActiveUser = iDataTotalService.selectUserEnterTotal(startTime, endTime);
            //今日新增收益
            BigDecimal currentIncome = iDataTotalService.selectIncome(startTime, endTime, param.getStoreId());
            String monthStart = CommonFunctionUtil.getStringTime(Constant.formatType.year);
            //本月收益
            BigDecimal monthIncome = iDataTotalService.selectIncome(monthStart + "-01 00:00:00", CommonFunctionUtil.getMonthLastDay() + " 23:59:59", param.getStoreId());

            //近15天的收入明细
            List<LpDataTotalTbl> incomeList = iDataTotalService.selectDataTotalList(15);
            //List<IncomeTotalRet> incomeListRet = DozerMapperUtil.mapList(incomeList, IncomeTotalRet.class);

            List<IncomeTotalRet> incomeListRet = new ArrayList<IncomeTotalRet>();
            for (int i = incomeList.size() - 1; i >= 0; i--) {

                IncomeTotalRet incomeTotalRet = DozerMapperUtil.map(incomeList.get(i), IncomeTotalRet.class);
                incomeListRet.add(incomeTotalRet);
            }

            IndexUserTotalRet indexUserTotalRet = new IndexUserTotalRet();
            indexUserTotalRet.setCurrentActiveUser(currentActiveUser);
            indexUserTotalRet.setCurrentIncome(currentIncome);
            indexUserTotalRet.setCurrentNewUser(currentNewUser);
            indexUserTotalRet.setMonthIncome(monthIncome);

            indexTotalRet.setUser(indexUserTotalRet);
            indexTotalRet.setIncomeList(incomeListRet);

            ret.setData(indexTotalRet);
            ret.setCode(ApiResponseEnum.API_RESPONSE_SUCCESS);
            ret.setMsg("查询成功");

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            ret.setMsg(e.getMessage());
        }

        return ret;
    }


    @RequestMapping(value = "/indexStoreTotal", method = RequestMethod.POST)
    @ApiOperation(notes = "主页门店数据统计", httpMethod = "POST", value = "主页门店数据统计")
    public BaseRet<List<IndexStoreTotalRet>> indexStoreTotal(@ApiParam(value = "请求参数", required = true) @Valid @RequestBody ActionLibraryInfoParam param,
                                                             BindingResult result) {

        BaseRet<List<IndexStoreTotalRet>> ret = new BaseRet<List<IndexStoreTotalRet>>();

        try {

            checkParam(result);
            List<LpStoreDataTotalTbl> list = iDataTotalService.selectStoreDataTotal(param.getId(), 15);

            //List<IndexStoreTotalRet> listRet = DozerMapperUtil.mapList(list, IndexStoreTotalRet.class);

            List<IndexStoreTotalRet> listRet = new ArrayList<IndexStoreTotalRet>();
            for (int i = list.size() - 1; i >= 0; i--) {
                IndexStoreTotalRet indexStoreTotalRet = DozerMapperUtil.map(list.get(i), IndexStoreTotalRet.class);
                listRet.add(indexStoreTotalRet);
            }

            ret.setData(listRet);
            ret.setCode(ApiResponseEnum.API_RESPONSE_SUCCESS);
            ret.setMsg("查询成功");

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            ret.setCode(ApiResponseEnum.API_RESPONSE_ERROR);
            ret.setMsg(e.getMessage());
        }

        return ret;
    }
}
